//
//  InlandController.m
//  FreeTrip
//
//  Created by 邓芮 on 15/11/24.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "InlandController.h"
#import "InlandCollectionCell.h"
#import "abAndeInModel.h"
#import "AreaTableViewController.h"
#import "BookPlaceDetailModel.h"


@interface InlandController ()<UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) NSArray *totalArray;

@end

@implementation InlandController

static NSString * const reuseIdentifier = @"Cell";

- (void)loadView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat width = (KScreenWidth - 30)/ 2;
    CGFloat height = 352 * width / 272;
    flowLayout.itemSize = CGSizeMake(width, height);
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView = collectionView;
    
    
    }

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[InlandCollectionCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    
    
    self.modelArray = [NSMutableArray array];
    if (![[NetworkState shareInstance]reachablity])
    {
        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"modelArray"])
        {
            self.totalArray = [self unArchiverModelArray];
            for (NSArray *arr in self.totalArray)
            {
                abAndeInModel *model = arr[0];
                [self.modelArray addObject:model];
            }
        }
        
    }else{
    
    
    [[NetworkState shareInstance] reachablityStates:self];

    [[NetworkRequest shareNetworeRequest]netRequestWithurl:KTotalUrl networkRequest:^(NSArray *array) {
        self.totalArray = array;
        if (![[NSUserDefaults standardUserDefaults]boolForKey:@"modelArray"])
        {
            [self archverArray:array];
        }
//        [self archverArray:array];
        
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"modelArray"];

        for (NSArray *arr in self.totalArray)
        {
            abAndeInModel *model = arr[0];
            [self.modelArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
        
    }];
    }
    
    // Do any additional setup after loading the view.
}

- (void)archverArray:(NSArray *)array
{
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:array forKey:@"modelArray"];
    [archiver finishEncoding];
    
    NSString *newPath = [[self docmentsPath] stringByAppendingPathComponent:@"modeArray.text"];
   // NSLog(@"%@",newPath);
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:newPath])
    {
        [manager createFileAtPath:newPath contents:nil attributes:nil];
    }
    [data writeToFile:newPath atomically:YES];
}
- (NSMutableArray *)unArchiverModelArray
{
     NSString *newPath = [[self docmentsPath] stringByAppendingPathComponent:@"modeArray.text"];
    NSData *data = [NSData dataWithContentsOfFile:newPath];
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSMutableArray *modelArray = [unArchiver decodeObjectForKey:@"modelArray"];
    return modelArray;
    
}

- (NSString *)docmentsPath
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AreaTableViewController *areaVc = [[AreaTableViewController alloc]init];
    
    areaVc.modelArray = self.totalArray[indexPath.item];
    
    [self.navigationController pushViewController:areaVc animated:NO];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    InlandCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
   
    
    
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
